
public class UserSimulator {

	Input nI;
	Input errI;
	boolean idealpres;
	ErrTyp errtyp;
	ModeErr modeerr;
	UnitErr uniterr;
	DataErr dataerr;
	int next_err;
	boolean errP;

	UserSimulator() {
		errtyp = new ErrTyp();
		nI = new Input();
		idealpres = nI.getNextPres();
		modeerr = new ModeErr();
		uniterr = new UnitErr(nI.m);
		errI = new Input(nI);
		dataerr = new DataErr(errI);
		next_err = errtyp.next_err();
		errP = false;
	}
	
	public boolean nextInput() {
		boolean done = false;
		errI = new Input(nI);
		while(!done) {
			if(!idealpres) {
				idealpres = true;
				return false;			
			} else {
				boolean err = errorpres();
				if(err) {
					errtyp.reset();
					next_err = errtyp.next_err();
					modeerr.reset();
					uniterr.reset();
					idealpres = nI.getNextPres();
					errP = false;
					if(idealpres) {
						done = true;
					}
					else {
						errI = new Input(nI);
						dataerr = new DataErr(errI);
						idealpres = true;
						return false;
					}
				}
				else {
//					System.out.println("err: " + err);
					errP = true;
					return err;
				}
			}
		}
		return done;
	}

	String Prestype() {
		if(errP) {
			return "_Err_" + errI.currentp + "_" + next_err ;
		} else {
			return "_Ideal_" + errI.currentp;
		}	
	}
	
	private boolean errorpres() {
		int done = -1;
		while(next_err != -1) {
			//System.out.println("nexterr: " + next_err);
			if(next_err == ErrTyp.MODE) {
				done = genmodeerror(nI.m);
				if(done != -1) {
					System.out.println("User: Gen mode err");
					return false;
				}
			}
			else if(next_err == ErrTyp.UNIT) {
				done = genuniterror();
				if(done != -1) {
					System.out.println("User: Gen Unit err");
					return false;
				}
			}
			else if(next_err == ErrTyp.DATA) {
				done = gendataerror();
				if(done != -1) {
					System.out.println("User: Gen Data err");
					return false;
				}
			}
			if(done == -1) {
				next_err = errtyp.next_err();
			}
		}
		return true;
	}
	
	private int gendataerror() {
		while(dataerr.rate_err != null && dataerr.rate_err.hasNext()) {
			errI.rate = dataerr.rate_err.next();
			System.out.println("rate: " + errI.rate);
			return 1;
		}
		while(dataerr.volume_err != null && dataerr.volume_err.hasNext()) {
			errI.volume = dataerr.volume_err.next();
			System.out.println("volume: " + errI.volume);
			return 1;
		}
		while(dataerr.conc_err != null && dataerr.conc_err.hasNext()) {
			errI.conc = dataerr.conc_err.next();
			System.out.println("conc" + errI.conc);
			return 1;
		}
		while(dataerr.dose_err != null && dataerr.dose_err.hasNext()) {
			errI.dose =  dataerr.dose_err.next();
			System.out.println("dose" + errI.dose);
			return 1;
		}
		return -1;	
	}

	private int genuniterror() {
		Units u = uniterr.next_err(nI.u, nI.m);
		if(u != null) {
			errI.u = u;
			return 1;
		}
		return -1;
	}

	private int genmodeerror(int m) {
		int err = modeerr.next_err(nI.m);
		if(err != -1)
		{
			errI.m = err;
			return 1;
		}
		return -1;	
	}
	public double getConc() {
		return errI.conc;
	}

	public double get_drug_amount() {
		return errI.drugamount;
	}

	public double get_dilutent_volume() {
		return errI.dilutent;
	}

	public double getVolume() {
		return errI.volume;
	}

	public double getDose() {
		return errI.dose;
	}

	public int getMode() {
		return errI.m;
	}

	public Units getUnits() {
		return errI.u;
	}

	public double getRate() {
		return errI.rate;
	}

	public Time getTime() {
		return errI.time;
	}
	public String org_prescription() {
		String str = new String();
		if(nI.rate != -1) {
			str += "Rate: " + nI.rate + "\n";
		}
		if(nI.volume != -1) {
			str += "Volume: " + nI.volume + "\n";			
		}
		if(nI.time.hours != -1) {
			str += "Time: " + nI.time.toString() + "\n";		
		}
		if(nI.conc != -1) {
			str += "Conc: " + nI.conc + "\n";
		}
		if(nI.dose != -1) {
			str += "Dose: " + nI.dose + "\n";	
		}
		if(nI.u != null) {
			str += "Units: " + nI.u.toString() + "\n";	
		}
		if(nI.weight != -1) {
			str += "Weight: " + nI.weight + "\n";
		}
		return str;
	}

	public String err_prescription() {
		String str = new String();
		str += "Errtyp: " + errtyp.toString() + "\n";
		if(errI.rate != -1) {
			str += "Rate: " + errI.rate + "\n";
		}
		if(errI.volume != -1) {
			str += "Volume: " + errI.volume + "\n";			
		}
		if(errI.time.hours != -1) {
			str += "Time: " + errI.time.toString() + "\n";		
		}
		if(errI.conc != -1) {
			str += "Conc: " + errI.conc + "\n";
		}
		if(errI.dose != -1) {
			str += "Dose: " + errI.dose + "\n";	
		}
		if(errI.u != null) {
			str += "Units: " + errI.u.toString() + "\n";	
		}
		if(errI.weight != -1) {
			str += "Weight: " + errI.weight + "\n";
		}
		return str;
	}

}
